package com.soft.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.soft.pojo.Dept;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/ajax")
public class AjaxServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Dept> depts = new ArrayList<>();
        depts.add(new Dept(10,"开发部","郑州"));
        depts.add(new Dept(11,"开发部1","郑州"));
        depts.add(new Dept(12,"开发部2","郑州"));
        depts.add(new Dept(13,"开发部3","郑州"));
        depts.add(new Dept(14,"开发部4","郑州"));
        //将集合转换成json，响应客户端
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(depts);
        resp.setContentType("application/json");

        PrintWriter out = resp.getWriter();
        out.print(json);
    }
}
